﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.BasicControl;
using Xinkui.Entity.AutoGenerator;
using CommonFramework.Utility;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel;
using Xinkui.WebModel.Helper;
using Xinkui.WebModel.Cache;
using Xinkui.Common.FileAction;
using Xinkui.Common.Enum;

public partial class UserControl_UserCenter_Seller_UC_PostProduct : AccountControl
{
    protected int Edit
    {
        get
        {
            return WebHelper.GetQueryStringValue<int>("e");
        }
    }

    public string CategoryName { get; set; }
    public int SelectCategory { get; set; }

    List<string> images = new List<string>(3);
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            images.Clear();
            var productInfo = Singleton<ProductInfoMaintainDataAccess>.Instance.ProductInfoMaintain_View(Edit);
            if (productInfo != null && productInfo.IsDeleted == false && productInfo.UserID == this.BasePage.CurrentLogin.UserID)
            {
                this.txtApplication.Text = productInfo.Application;
                this.txtDescription.Text = productInfo.ProductDescription;
                this.txtKeywords.Text = productInfo.Keywords;
                this.txtOEMNo.Text = productInfo.OEMNO;
                this.txtProductName.Text = productInfo.ProductName;
                SelectCategory = productInfo.CategoryID;
                var cInfo = Singleton<CommonCategoryDataAccess>.Instance.CommonCategory_View(SelectCategory);
                CategoryName = cInfo != null ? cInfo.Title : string.Empty;
                images = FileDataAccess.GetFileListByType(Edit, (int)ESourceType.FromProductImage).Select(u => Xinkui.Common.FileUpload.GetDirectoryPath(u.FilePath, ESourceType.FromProductImage)).ToList();
                this.btnDraft.Visible = ((productInfo.AuditStatus & ((int)(AuditStatusEnum.New | AuditStatusEnum.Draft))) > 0);

            }
            while (images.Count < 3)
            {
                images.Add(string.Empty);
            }
        }

        rptImageScripts.DataSource = images;
        rptImageScripts.DataBind();
    }

    protected void Save(int status)
    {

        ProductInfoMaintain_Entity entity = new ProductInfoMaintain_Entity();
        DateTime n = DateTime.Now;
        entity.CreateDate = n;
        entity.CreateIP = WebHelper.UserIP;
        entity.AuditDate = n;
        entity.AuditStatus = status == 0 ? (int)AuditStatusEnum.New : status;
        if (Edit > 0)
        {
            entity = Singleton<ProductInfoMaintainDataAccess>.Instance.ProductInfoMaintain_View(Edit);
            if (entity == null || entity.CompanyID != this.BasePage.CurrentLogin.CompanyID)
            {
                UIHelper.ShowErrorTips("The product infomation has error");
                return;
            }
            if (status != 0)
            {
                if (entity.AuditStatus == (int)AuditStatusEnum.New || entity.AuditStatus == (int)AuditStatusEnum.Draft)
                {
                    entity.AuditStatus = status;
                }
                else
                {
                    entity.AuditStatus = (int)AuditStatusEnum.Change;
                }
            }
            else
            {
                if (entity.AuditStatus != (int)AuditStatusEnum.New && entity.AuditStatus != (int)AuditStatusEnum.Draft)
                {
                    entity.AuditStatus = (int)AuditStatusEnum.New;
                }
                else
                {
                    entity.AuditStatus = (int)AuditStatusEnum.Change;
                }
            }
        }
        int category = WebHelper.GetFormValue<int>("hidcategory", 0);
        entity.UpdateDate = n;
        entity.CategoryID = category;
        entity.UpdateIP = WebHelper.UserIP;
        entity.UserID = this.BasePage.CurrentLogin.UserID;
        entity.Application = this.txtApplication.Text;
        entity.CompanyID = this.BasePage.CurrentLogin.CompanyID;
        entity.IsDeleted = false;
        entity.Keywords = this.txtKeywords.Text;
        entity.MainKeyword = this.txtKeywords.Text;
        entity.OEMNO = this.txtOEMNo.Text;
        entity.ProductDescription = this.txtDescription.Text;
        entity.ProductName = this.txtProductName.Text;
        string attachment = WebHelper.GetFormValue<string>("hidfilepath", string.Empty);
        List<int> attachIds = new List<int>(0);
        if (!string.IsNullOrEmpty(attachment))
            attachIds = attachment.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(u => ConvertHelper.ToType<int>(System.IO.Path.GetFileNameWithoutExtension(u))).ToList();
        images.Clear();
        images = attachment.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

        rptImageScripts.DataSource = images;
        rptImageScripts.DataBind();

        entity.PicImgs = attachment;


        if (Edit > 0)
        {
            var fileList = FileDataAccess.GetFileListByType(Edit, (int)ESourceType.FromProductImage);

            foreach (var del in fileList.Where(u => !attachIds.Contains(u.FileID)))
            {
                FileDataAccess.DeleteFileByID(del.FileID);
            }

            Singleton<ProductInfoMaintainDataAccess>.Instance.ProductInfoMaintain_Update(entity);
        }
        else
        {

            entity = Singleton<ProductInfoMaintainDataAccess>.Instance.ProductInfoMaintain_Add(entity);
        }

        foreach (var attachid in attachIds)
            Xinkui.Common.FileUpload.UpdateAttachment(attachid, Xinkui.Common.Enum.ESourceType.FromProductImage, entity.ProductID);

        UIHelper.ShowSuucessTips("Product Infomation Saved", URLHelper.LinkToManagerProduct());
    }

    protected void btnSubmit_Click(object o, EventArgs e)
    {
        Save(0);
    }


    protected void btnDraft_Click(object o, EventArgs e)
    {
        Save((int)AuditStatusEnum.Draft);
    }
}